﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using System.Data;
using System.IO;

namespace TimKen.Web
{
    public partial class Resource : WebPage
    {
        ResourceManager rm = new ResourceManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind("");
            }
        }

        public void Bind(string where)
        {
            DataSet ds = rm.GetPrdctFilePageList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, where);

            repResource.DataSource = ds.Tables[0].DefaultView;
            repResource.DataBind();
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);
        }

        public long GetSizeByName(string name)
        {
            string path = ResourcePath + name;
            FileInfo file = new FileInfo(Server.MapPath(path));


            string key = "file" + name;
            object size = Cache.Get(key);

            if (size == null)
            {
                if (!file.Exists)
                    size = 0;
                else
                    size = file.Length;
                Cache.Insert(key, size);
                return long.Parse(size.ToString());
            }
            return long.Parse(size.ToString());
        }

        protected void repResource_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Down")
            {
                try
                {
                    string path = ResourcePath + e.CommandArgument.ToString();
                    string filename = System.IO.Path.GetFileName(e.CommandArgument.ToString());

                    if (System.IO.File.Exists(Server.MapPath(path)))
                    {

                        string filePath = Server.MapPath(path);//路径 

                        //以字符流的形式下载文件 
                        FileStream fs = new FileStream(filePath, FileMode.Open);
                        byte[] bytes = new byte[(int)fs.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        fs.Close();
                        Response.ContentType = "application/octet-stream";
                        //通知浏览器下载文件而不是打开 
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
                        Response.Flush();
                        Response.BinaryWrite(bytes);
                    }
                    else
                    {
                        Alert(GetLangString("NotFoundFile"));
                    }
                }
                catch
                {
                    Response.Redirect("/Error.aspx");
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind(txt_ssp.Value);
        }
        /// <summary>
        /// 搜索资源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_ssd_Click(object sender, EventArgs e)
        {
            Bind(txt_ssp.Value);
        }
    }
}
